package com.sugar.subscribe.course.module.business.controller.app.order;

import com.sugar.subscribe.course.framework.common.pojo.CommonResult;
import com.sugar.subscribe.course.framework.common.pojo.PageResult;
import com.sugar.subscribe.course.framework.security.core.util.SecurityFrameworkUtils;
import com.sugar.subscribe.course.module.business.controller.app.order.vo.*;
import com.sugar.subscribe.course.module.business.service.order.OrderService;
import com.sugar.subscribe.course.module.pay.api.order.dto.PayOrderSubmitRespDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import static com.sugar.subscribe.course.framework.common.pojo.CommonResult.success;
import static com.sugar.subscribe.course.framework.common.util.servlet.ServletUtils.getClientIP;

@Tag(name = "APP - 订单")
@RestController
@RequestMapping("/biz/order")
@Validated
public class AppOrderController {

    @Resource
    private OrderService orderService;

    @PostMapping("/create")
    @Operation(summary = "用户端 - 创建订单")
    public CommonResult<Boolean> createOrder(@Valid @RequestBody AppOrderCreateReqVO createReqVO) {
        orderService.appCreateOrder(createReqVO, SecurityFrameworkUtils.getLoginUserId(false));
        return success(true);
    }

    @PostMapping("/pay")
    @Operation(summary = "用户端 - 支付订单")
    public CommonResult<PayOrderSubmitRespDTO> appUserPayOrder(@Valid @RequestBody AppOrderIdReqVO payReqVO) {
        return success(orderService.appUserPayOrder(payReqVO.getId(), SecurityFrameworkUtils.getLoginUserId(false), getClientIP()));
    }

    @PostMapping("/update")
    @Operation(summary = "用户端 - 更新订单需求")
    public CommonResult<Boolean> updateOrder(@Valid @RequestBody AppOrderUpdateReqVO updateReqVO) {
        orderService.appUpdateOrder(updateReqVO, SecurityFrameworkUtils.getLoginUserId(false));
        return success(true);
    }

    @GetMapping("/page/user")
    @Operation(summary = "用户端 - 获得订单分页")
    public CommonResult<PageResult<AppUserOrderPageRespVO>> getUserOrderPage(@Valid AppOrderPageReqVO reqVO) {
        return success(orderService.appGetUserOrderPage(reqVO, SecurityFrameworkUtils.getLoginUserId(false)));
    }

    @GetMapping("/page/coach")
    @Operation(summary = "教练端 - 获得订单分页")
    public CommonResult<PageResult<AppUserOrderPageRespVO>> getCoachOrderPage(@Valid AppOrderPageReqVO reqVO) {
        return success(orderService.appGetCoachOrderPage(reqVO, SecurityFrameworkUtils.getLoginUserId(true)));
    }

    @PostMapping("/cancel/user")
    @Operation(summary = "用户端 - 取消订单")
    public CommonResult<Boolean> userCancelOrder(@Valid @RequestBody AppOrderIdReqVO cancelReqVO) {
        orderService.appUserCancelOrder(cancelReqVO.getId(), SecurityFrameworkUtils.getLoginUserId(false));
        return success(true);
    }

    @PostMapping("/cancel/coach")
    @Operation(summary = "教练端 - 取消订单")
    public CommonResult<Boolean> coachCancelOrder(@Valid @RequestBody AppOrderIdReqVO cancelReqVO) {
        orderService.appCoachCancelOrder(cancelReqVO.getId(), SecurityFrameworkUtils.getLoginUserId(true));
        return success(true);
    }

    @PostMapping("/done")
    @Operation(summary = "用户端 - 完成订单")
    public CommonResult<Boolean> doneOrder(@Valid @RequestBody AppOrderIdReqVO doneReqVO) {
        orderService.appDoneOrder(doneReqVO.getId(), SecurityFrameworkUtils.getLoginUserId(false));
        return success(true);
    }

    @PostMapping("/take/order")
    @Operation(summary = "教练端 - 接单")
    public CommonResult<Boolean> coachTakeOrder(@Valid @RequestBody AppOrderIdReqVO takeOrderReqVO) {
        orderService.appCoachTakeOrder(takeOrderReqVO.getId(), SecurityFrameworkUtils.getLoginUserId(true));
        return success(true);
    }

    @PostMapping("/attend/class")
    @Operation(summary = "教练端 - 上课")
    public CommonResult<Boolean> coachAttendClassOrder(@Valid @RequestBody AppOrderAttendClassReqVO appOrderAttendClassReqVO) {
        orderService.appCoachAttendClassOrder(appOrderAttendClassReqVO, SecurityFrameworkUtils.getLoginUserId(true));
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "公共 - 获得订单详情")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<AppOrderDetailRespVO> getOrder(@RequestParam("id") Long id) {
        return success(orderService.appGetOrder(id));
    }
}